package com.jswdwsx.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class MapUtils {
    /**
     * 过滤Map中的无意义项
     *
     * @param map
     * @param <T>
     * @return
     */
    public static <T> Map<String, T> parseMapForFilterByOptional(Map<String, T> map) {
        return Optional.ofNullable(map).map(
                (v) -> v.entrySet().stream()
                        .filter((e) -> checkValue(e.getValue()))
                        .collect(Collectors.toMap(
                                (e) -> (String) e.getKey(),
                                Map.Entry::getValue
                        ))
        ).orElse(null);
    }

    private static <T> boolean checkValue(T t) {

        if (t instanceof String && "".equals(t)) {
            return false;
        }

        return null != t;
    }

    public static <K, V> HashMap<K, V> newHashMap(Integer size) {
        return new HashMap<>(Double.valueOf(Math.floor(size / 0.75)).intValue() + 1);
    }
}
